unit Core.ParamList;

interface

uses
  SysUtils, Generics.Collections, System.Classes;

type
  TclValues = class;
  TclParam = class;
  TStrs = TArray<String>;
  TIntegers = TArray<Integer>;
  TFloats = TArray<Double>;
  TBooleans = TArray<Boolean>;
  TRawValues = TList<string>;
  TclParams = TObjectList<TclParam>;

  IclRawValues = interface
    function GetRawValues : TRawValues;
  end;

  IclParamTypSetter = interface(IInterface)
    procedure SetTyp(const typ: string);
  end;

  TclValues = class(TInterfacedObject, IclRawValues)
  private
    fRawValues : TRawValues;
    function GetRawValues : TRawValues;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  public
    constructor Create;
    destructor Destroy; override;
    function AsInteger: Integer;
    function AsIntegers: TIntegers;
    function AsString: string;
    function AsStrings: TStrs;
  end;

  TclParam = class(TInterfacedPersistent, IclParamTypSetter)
  strict private
    procedure SetTyp(const typ: string);
  private
    fName: string;
    fTyp: string;
    fValues: TclValues;
  public
    constructor Create(const name, typ: string);
    destructor Destroy; override;
    property Name: string read fName;
    property Typ: string read fTyp;
    property Values: TclValues read fValues;
  end;

implementation

constructor TclValues.Create;
begin
  inherited;
  fRawValues := TRawValues.Create;
end;

destructor TclValues.Destroy;
begin
  inherited;
  FreeAndNil(fRawValues);
end;

function TclValues.AsInteger: Integer;
begin
  if fRawValues.Count > 0 then
    Result := StrToInt(fRawValues.First)
  else
    Result := 0;
end;

function TclValues.AsIntegers: TIntegers;
var
  intList: TIntegers;
  i: Integer;
begin
  SetLength(intList, fRawValues.Count);

  for i := 0 to fRawValues.Count - 1 do
  begin
    intList[i] := StrToInt(fRawValues[i]);
  end;

  Result := intList;
end;

function TclValues.AsString: string;
begin
  if fRawValues.Count > 0 then
    Result := fRawValues.First
  else
    Result := '';
end;

function TclValues.AsStrings: TStrs;
begin
  Result := fRawValues.ToArray;
end;

function TclValues.GetRawValues: TRawValues;
begin
  Result := fRawValues;
end;

function TclValues._AddRef: Integer;
begin
  Result := -1;
end;

function TclValues._Release: Integer;
begin
  Result := -1;
end;

constructor TclParam.Create(const name, typ: string);
begin
  fName := name;
  fValues := TclValues.Create;
end;

destructor TclParam.Destroy;
begin
  FreeAndNil(fValues);
end;

procedure TclParam.SetTyp(const typ: string);
begin
  fTyp := typ;
end;

end.

